package com.hushan.elevator.controller;

import com.hushan.elevator.model.Department;
import com.hushan.elevator.model.RiskRecord;
import com.hushan.elevator.model.User;
import com.hushan.elevator.security.SecurityUtils;
import com.hushan.elevator.service.*;
import com.hushan.elevator.utils.Result;
import com.hushan.elevator.utils.ResultCode;
import com.hushan.elevator.vo.RiskStaticsVo;
import com.hushan.elevator.vo.StatisticsVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RequestMapping("/statics")
@RestController
@Tag(name = "StaticsController", description = "统计信息接口")
public class StaticsController {
    private final StaticsService staticsService;
    private final DeviceInfoService deviceInfoService;
    private final DeptService deptService;
    private final RiskRecordService riskService;

    public StaticsController(StaticsService staticsService, DeviceInfoService deviceInfoService, DeptService deptService, RiskRecordService riskService) {
        this.staticsService = staticsService;
        this.deviceInfoService = deviceInfoService;
        this.deptService = deptService;
        this.riskService = riskService;
    }


    @Operation(summary = "获取当前统计信息", description = "包含设备数量、在线设备数量、离线设备数量、维保人员数量等信息")
    @GetMapping("/home")
    public Result getDeptStaticsInfo() {
        return new Result(ResultCode.SUCCESS, staticsService.getHomeStatics());
    }

    @Operation(summary = "获取当前部门风险信息", description = "前5种类最多的风险类型")
    @GetMapping("/home/risk")
    public Result getRiskStatics() {
        List<Object[]> statics = staticsService.getRiskStatics();
        if (statics.size()<5){
            return new Result(ResultCode.SUCCESS,statics);
        }
        return new Result(ResultCode.SUCCESS, statics.subList(0, 5));
    }

    @Operation(summary = "获取当前子部门的各自的风险数量", description = "获取当前子部门的各自的风险数量")
    @GetMapping("/home/dept/info")
    public Result getChildDeptStatics() {
        User requester = SecurityUtils.getUser();
        Department dept = deptService.findDeptByUserId(requester.getUserId());
        List<Department> childDept = deptService.findChildByDeptId(dept.getDeptId());
        childDept.add(dept);
        String[] deptName = new String[childDept.size()];
        int[] riskNumb = new int[childDept.size()];
        int i = 0;
        for (Department item : childDept) {
            riskNumb[i] = riskService.countByDept(item.getDeptId());
            deptName[i] = item.getName();
            i++;
        }
//        Arrays.copyOf(deptName,6),Arrays.copyOf(riskNumb,6)
        return new Result(ResultCode.SUCCESS, deptName,riskNumb);
    }
}
